<?php
  // no direct access
  defined( '_JEXEC' ) or die( 'Restricted access' );
  jimport( 'joomla.application.component.view');
  
  class PlayerViewPlayer extends JView
  {              
    var $_name = "player";
  
    function display()
    { 
      $this->displayList();
    }
    
    function displayList()
    {
      /* sorting */
      $state =& $this->get( 'state' );
            $lists['order_Dir'] = $state->get($this->_name . 'filter_order_Dir');
            $lists['order']     = $state->get($this->_name . 'filter_order');
            $this->assignRef('lists', $lists);    
    
      $player   = $this->getModel('player');
      $person   = $this->getModel('person');
      $team     = $this->getModel('team');
      $position = $this->getModel('position');
      
      $players = $player->get_summary_list();
      $this->assign('players', $players);
      
      $pagination = $this->get('Pagination');
      $this->assign('pagination', $pagination);
      
      JToolBarHelper::title( JText::_( 'Players' ), 'generic.png' );
      
      JToolBarHelper::publishList();
      JToolBarHelper::unpublishList();
      JToolBarHelper::addNew();
      JToolBarHelper::editList();
      JToolBarHelper::deleteList();
      
      PBNYHelper::createMenu();
      
      parent::display("list");    
    }
    
    function displayAdd()
    {  
      $player   = $this->getModel('player');
      $person   = $this->getModel('person');
      $position = $this->getModel('position');
      $pic      = $this->getModel('pic');
      $division = $this->getModel('division');
      
      $this->sort_teams_by_division();
      //$this->assign('teams', $this->sort_teams_by_division());
      $this->assign('persons', $person->get_fullnames());
      $this->assign('positions', $position->get());
      $this->assign('divisions', $division->get());
      
      JToolBarHelper::title( JText::_( 'Add Player' ), 'generic.png' );
      JToolBarHelper::save();
      JToolBarHelper::cancel();  

      parent::display("edit");
    }    
    
    function displayEdit()
    {    
      $player   = $this->getModel('player');
      $person   = $this->getModel('person');
      $team     = $this->getModel('team');
      $position = $this->getModel('position');
      $pic      = $this->getModel('pic');
      $division = $this->getModel('division');
      
      $cids = JRequest::getVar('cid', null, 'default', 'array' );
      $player_obj = $player->get($cids[0]);
      
      if(isset($player_obj)){
        $person_obj   = $person->get($player_obj->person_id);
        if($player_obj->pic_id){
          $pic_obj    = $pic->get($player_obj->pic_id);
          $this->assign('pic', $pic_obj);
        }
        
        $pics = $pic->get_pics_by_instance('PLAYERS', $cids[0]);
        if($pics){
          foreach($pics as $p){
            if($p->id == $player_obj->pic_id)
              $pic_obj  = $p;
            else{
              $feature_pic = $p;
              $this->assign('feature_pic', $feature_pic);
            }
          }
        }
        
        $this->assign('person', $person_obj);
      }
      
      $this->assign('player', $player_obj);
      $this->assign('persons', $person->get());
      $this->assign('teams', array($team->get($player_obj->team_id)));
      $this->assign('positions', $position->get());
      $this->assign('divisions', $division->get());
      
      JToolBarHelper::title( JText::_( 'Edit Player' ), 'generic.png' );
      JToolBarHelper::save();
      JToolBarHelper::cancel();  

      parent::display("edit");
    }    
  
    private function sort_teams_by_division()
    {
      $result = Array();
      
      $season              = $this->getModel('season');
      $team                = $this->getModel('team');
      $season_division     = $this->getModel('seasondivision');
      $division_conference = $this->getModel('divisionconference');
      $conference_team     = $this->getModel('conferenceteam');
      
      $current_season = $season->get_latest();
      $season_divisions = $season_division->get_by_season_id($current_season->id);
      
      # loop for divisions
      foreach($season_divisions as $sd){
        if(!isset($result[$sd->division_id]))
          $result[$sd->division_id] = Array();
        
        #loop for conferences
        $division_conferences = $division_conference->get_by_division_id($sd->division_id);
        foreach($division_conferences as $dc){
          $conference_teams = $conference_team->get_by_conference_id($dc->conference_id);
          foreach($conference_teams as $ct)
            $result[$sd->division_id][$ct->team_id] = $team->get($ct->team_id);
        }
      }
      return $result;
    }
  }
?>